﻿using System;
using System.Globalization;
using System.Linq;
using System.Reflection;
using System.Text.RegularExpressions;
using Caliburn.Micro;
using MessageLib;
using MessageLib.Frame;
using MessageLib.Frame.Base;
using VoiceMessager.Models;

namespace VoiceMessager.ViewModels
{
	public sealed class SendFrameViewModel : Screen
	{
		private readonly CurrentSelection _currentSelection;
	    private MessageFrame _currentMessageFrame;

		public SendFrameViewModel()
		{
		}
        
		public SendFrameViewModel(CurrentSelection currentSettings)
		{
			DisplayName = @"报文编辑";
			_currentSelection = currentSettings;
            UpdateFrame();
            NotifyAll();
		}

	    public string FrameHead1
	    {
	        get => _currentMessageFrame.Head [0].ToString("X2");
	        set
	        {
	            
	        }
	    }
    public bool FrameHead1Test { get; set; }

		public string FrameHead2 => _currentSelection.MessageFrame.Head[1].ToString("X2");
        public bool FrameHead2Test { get; set; }

		public string FrameHead3 => _currentSelection.MessageFrame.Head[2].ToString("X2");
        public bool FrameHead3Test { get; set; }

		public string FrameOrder => _currentSelection.MessageFrame.Order[0].ToString("X2");
        public bool FrameOrderTest { get; set; }

		public string FrameSum => _currentSelection.MessageFrame.Sum[0].ToString("X2");
        public bool FrameSumTest { get; set; }

		public string FrameVerifyMethod => _currentSelection.MessageFrame.VerifyMethod[0].ToString("X2");
        public bool FrameVerifyMethodTest { get; set; }

		public string CommandLength => _currentSelection.MessageFrame.DataLength[0].ToString("X2");
        public bool CommandLengthTest { get; set; }

		public string FrameVerifyByte1 => _currentSelection.MessageFrame.VerifyData[0].ToString("X2");
        public bool FrameVerifyByte1Test { get; set; }

		public string FrameVerifyByte2 => _currentSelection.MessageFrame.VerifyData[1].ToString("X2");
        public bool FrameVerifyByte2Test { get; set; }

		public string StopWord => _currentSelection.MessageFrame.Stop[0].ToString("X2");
        public bool StopWordTest { get; set; }

		public string CommandDetail => Statics.HexFormating(_currentSelection.MessageFrame.FrameData,3);
        public bool CommandDetailTest{ get; set; }

        public string MacText { get; set; }
	    public bool MacTextTest => _macTextTest(MacText);
        public byte[] MacBytes { get; set; }
	    private bool _macTextTest(string macText)
	    {
	        if (!Regex.IsMatch(macText, Statics.MacRegex)) return false;
	        try
	        {
	            MacBytes = macText.Split(':').Select(i => byte.Parse(i, NumberStyles.HexNumber)).ToArray();
	            return true;
	        }
	        catch (Exception)
	        {
	            return false;
	        }
	    }

        public string IpText { get; set; }
	    public bool IpTextTest => _ipTextTest(IpText);
        public byte[] IpBytes { get; set; }
	    private bool _ipTextTest(string ipText)
	    {
	        if (!Regex.IsMatch(ipText, Statics.IpRegex)) return false;
	        try
	        {
	            IpBytes = ipText.Split('.').Select(i => Convert.ToByte(i)).ToArray();
	            return true;
	        }
	        catch (Exception)
	        {
	            return false;
	        }
	    }

	    public string MaskText { get; set; }
	    public bool MaskTextTest => _maskTextTest(MaskText);
        public byte[] MaskBytes { get; set; }
	    private bool _maskTextTest(string maskText)
	    {
	        if (!Regex.IsMatch(maskText, Statics.IpRegex)) return false;
	        try
	        {
	            MaskBytes = maskText.Split('.').Select(i => Convert.ToByte(i)).ToArray();
                return true;
	        }
	        catch (Exception)
	        {
	            return false;
	        }
	    }

	    public string GatewayText { get; set; }
	    public bool GatewayTextTest => _gatewayTextTest(MaskText);
        public byte[] GatewayBytes { get; set; }
	    private bool _gatewayTextTest(string gatewayText)
	    {
	        if (!Regex.IsMatch(gatewayText, Statics.IpRegex)) return false;
	        try
	        {
	            GatewayBytes = gatewayText.Split('.').Select(i => Convert.ToByte(i)).ToArray();
                return true;
	        }
	        catch (Exception)
	        {
	            return false;
	        }
	    }

		public string VoiceCommandText { get; set; }

	    public FrameType FrameTypeSelected { get; set; }
        private int _tabSelected;
	    public int TabSelected
	    {
	        get => _tabSelected;
	        set
	        {
	            _tabSelected = value;
	            switch (TabSelected)
	            {
	                case 0:
	                    FrameTypeSelected = FrameType.ControlFrame;
	                    break;
	                case 1:
	                    FrameTypeSelected = FrameType.SettingFrame;
	                    break;
	                case 2:
	                    FrameTypeSelected = FrameType.StatusFrame;
	                    break;
	                case 3:
	                    FrameTypeSelected = FrameType.FreeFrame;
	                    break;
	            }
            }
	    }

	    
	
	    public void SendFrame()
		{
		}

		

		public bool CanSendFrame
	    {
	        get
	        {
	            switch (_currentSelection.CommandFrameType)
	            {
	                case FrameType.ControlFrame:
	                    if (_currentSelection.ControlCommand == ControlCommand.PlayVoice)
	                    {
	                        return VoiceCommandText.Length > 0;
	                    }
	                    return true;
	                case FrameType.SettingFrame:
	                    return MacTextTest && IpTextTest && MaskTextTest && GatewayTextTest;
                    case FrameType.StatusFrame:
	                    return false;
                    default:
	                    return false;
	            }
	        }
	    }

	    public void CancelAction()
		{
			TryClose();
		}

	    public void Insert(string s)
	    {
	        VoiceCommandText = s;
	    }


		public void NotifyAll()=>typeof(SendFrameViewModel)
                                    .GetProperties(BindingFlags.GetProperty|BindingFlags.Public)
                                    .ToList()
                                    .ForEach(i=>NotifyOfPropertyChange(i.Name));


	    public void UpdateFrame()
	    {
	        CommandFrame commandFrame;
	        switch (FrameTypeSelected)
	        {
	            case FrameType.ControlFrame:
	                switch (_currentSelection.ControlCommand)
	                {
	                    case ControlCommand.PlayVoice:
	                        commandFrame = CommandFrame.CreateControlCommandFrame(ControlCommand.PlayVoice,
	                            _currentSelection.ControlCommandParam, _currentSelection.VoiceText);
	                        break;
	                    default:
	                        commandFrame = CommandFrame.CreateControlCommandFrame(_currentSelection.ControlCommand);
	                        break;
	                }
	                break;
	            case FrameType.SettingFrame:
	                commandFrame = CommandFrame.CreateSettingCommandFrame(_currentSelection.SetMac,
	                    _currentSelection.SetIp, _currentSelection.SetMask, _currentSelection.SetGateway);
	                break;
	            case FrameType.StatusFrame:
	                commandFrame = null;
	                break;
	            default:
	                commandFrame = null;
	                break;
	        }
	        _currentSelection.MessageFrame = MessageFrame.CreateInstance(commandFrame, _currentSelection.VerifyMethod);
	    }
	}
}
